package com.yuanlrc.base.controller.admin;

import com.yuanlrc.base.bean.CodeMsg;
import com.yuanlrc.base.bean.Result;
import com.yuanlrc.base.entity.admin.BookCategory;
import com.yuanlrc.base.service.admin.BookCategoryService;
import com.yuanlrc.base.service.admin.OperaterLogService;
import com.yuanlrc.base.util.BookCatgoryUtil;
import com.yuanlrc.base.util.ValidateEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 图书分类控制器
 */
@Controller
@RequestMapping("/category")
public class BookCategoryController  {
    @Autowired
    private BookCategoryService bookCategoryService  ;
   @Autowired
    private OperaterLogService operaterLogService;

    /**
     * 图书列表展示页面
     * @param model
     * @return
     */
    @RequestMapping(value="/list")
    public String list(Model model){
        List<BookCategory> findAll = bookCategoryService.findAll();
        model.addAttribute("title","图书分类列表");
        model.addAttribute("topBcs", BookCatgoryUtil.getTopBSs(findAll));
        model.addAttribute("secondBcs",BookCatgoryUtil.getSecondBcs(findAll));

//        model.addAttribute("thirdMenus",MenuUtil.getThirdMenus(findAll));
        return "admin/category/list";
    }

    /**
     * 图书分类添加页面
     * @param model
     * @return
     */
    @RequestMapping(value="/add",method= RequestMethod.GET)
    public String add(Model model){
        List<BookCategory> findAll = bookCategoryService.findAll();
        model.addAttribute("title","图书分类添加");
        model.addAttribute("topBcs",BookCatgoryUtil.getTopBSs(findAll));
        model.addAttribute("secondBcs",BookCatgoryUtil.getSecondBcs(findAll));
        return "admin/category/add";
    }

    /**
     * 图书分类添加提交表单处理
     * @param bookCategory
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> add(BookCategory bookCategory){

        if(bookCategory == null){
            Result.error(CodeMsg.DATA_ERROR);
        }
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(bookCategory);

        if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        if(bookCategory.getParent() != null){
            if(bookCategory.getParent().getId() == null){
                bookCategory.setParent(null);
            }
        }

        if(BookCatgoryUtil.isExistTypeName(bookCategory.getTypeName(),bookCategoryService.findAll()))
            return Result.error(CodeMsg.ADMIN_CATEGORY_ADD_ERROR);

        //表示验证都通过，开始添加数据库
        if(bookCategoryService.save(bookCategory) == null){
            Result.error(CodeMsg.ADMIN_CATEGORY_ADD_ERROR);
        }
        //数据库添加操作成功,记录日志
        operaterLogService.add("添加类型信息【" + bookCategory + "】");
        return Result.success(true);
    }

    /**
     * 图书分类编辑页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.GET)
    public String eidt(Model model,@RequestParam(name="id",required=true)Long id){
        List<BookCategory> findAll = bookCategoryService.findAll();
        model.addAttribute("title","图书列表");
        model.addAttribute("topBcs",BookCatgoryUtil.getTopBSs(findAll));
        model.addAttribute("secondBcs",BookCatgoryUtil.getSecondBcs(findAll));
        model.addAttribute("category",bookCategoryService.find(id));
        return "admin/category/edit";
    }

    /**
     * 图书分类编辑页面表单提交处理
     * @param
     * @param bookCategory
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> edit(BookCategory bookCategory){
        if(bookCategory == null){
            Result.error(CodeMsg.DATA_ERROR);
        }
        if(bookCategory.getId() == null){
            Result.error(CodeMsg.ADMIN_MENU_ID_EMPTY);
        }
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(bookCategory);
        if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        if(bookCategory.getParent() != null){
            if(bookCategory.getParent().getId() == null){
                bookCategory.setParent(null);
            }
        }
        BookCategory existBc = bookCategoryService.find(bookCategory.getId());
        if(existBc == null){
            Result.error(CodeMsg.ADMIN_MENU_ID_ERROR);
        }

        if(existBc.getParent() != null)
        {
            if(bookCategory.getParent() == null)
                return Result.error(CodeMsg.ADMIN_EDIT_BOOK_CATEGORY_ERROR);
        }


        if(bookCategory.getTypeName().equals(existBc.getTypeName()))
        {
            if(bookCategory.getParent() != null && existBc.getParent() != null
                    && bookCategory.getParent().getId() == existBc.getParent().getId())
            {
                return Result.success(true);
            }
        }
        else
        {
            if (BookCatgoryUtil.isExistTypeName(bookCategory.getTypeName(), bookCategoryService.findAll()))
                return Result.error(CodeMsg.ADMIN_CATEGORY_UPDATE_ERROR);
        }

        //表示验证都通过，开始添加数据库
        existBc.setParent(bookCategory.getParent());
        existBc.setTypeName(bookCategory.getTypeName());
        if(bookCategoryService.save(existBc) == null){
            Result.error(CodeMsg.ADMIN_MENU_ADD_ERROR);
        }
        //数据库添加操作成功,记录日志
        operaterLogService.add("编辑菜单信息【" + existBc + "】");
        return Result.success(true);
    }

    /**
     * 删除图书分类信息
     * @param
     * @param id
     * @return
     */
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){
        try {
            bookCategoryService.delete(id);
        } catch (Exception e) {
            return Result.error(CodeMsg.ADMIN_MENU_DELETE_ERROR);
        }
        //数据库添加操作成功,记录日志
        operaterLogService.add("删除菜单信息，菜单ID【" + id + "】");
        return Result.success(true);
    }

}
